<template>
  <div class="layout">
    <!-- <el-container> -->
    <!-----------------header----------------->
    <el-header height="64px">
      <!--对应医院名称-->
      <div class="logo">
        <img :src="hospLogo" />
        <span>{{ hospName }}</span>
      </div>
      <!--用户登录信息-->
      <ul>
        <li>
          <Icon type="md-person" />
          <span>欢迎您，{{ nickname }}</span>
        </li>
        <li @click="quit">
          <Icon type="ios-power" />
          <span>退出系统</span>
        </li>
      </ul>
    </el-header>
    <!-----------------sider&content----------------->
    <el-container style="height: 900px;">
      <!--sider-->
      <el-aside width="210px">
        <el-menu unique-opened
                 :collapse-transition="false"
                 router
                 :default-active="activePathName"
                 background-color="#333744"
                 text-color="#fff"
                 active-text-color="#409FFF">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''"
                      v-for="item in menu"
                      :key="item.id">
            <template slot="title">
              <span>{{ item.text }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.href"
                          v-for="subItem in item.children"
                          :key="subItem.id"
                          @click="saveNavState('/' + subItem.href)"
                          style="padding-left:20px">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.text }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--content-->
      <el-main>
        <div class="page">
          <div class="bg_f">
            <el-breadcrumb class="title">
              <el-breadcrumb-item v-for="item in $route.matched"
                                  :key="item.name"
                                  :to="item.path">{{ item.meta.title }}
              </el-breadcrumb-item>
            </el-breadcrumb>
            <router-view />
          </div>
        </div>
      </el-main>
    </el-container>
    <!-- </el-container> -->
  </div>
</template>

<script>
  export default {
    name: 'mainframe',
    data () {
      return {
        hospLogo: 'static/images/logo.png',
        hospName: '天下医生智慧数字化医疗平台',
        nickname: JSON.parse(sessionStorage.getItem('userName')),
        userId: JSON.parse(sessionStorage.getItem('id')),
        userName: JSON.parse(sessionStorage.getItem('userName')),
        token: JSON.parse(sessionStorage.getItem('token')),
        menu: [],
        // 被激活导航地址
        activePath: '',
      };
    },
    computed: {
      activePathName () {
        return '/' + this.$route.name;
      },
    },
    created () {

      this.activePath = window.sessionStorage.getItem('activePath');
      // 菜单
      this.post(this.api.menu, {})
        .then(res => {
          if (res.data.code === '200') {
            this.menu = res.data.object;
            localStorage.setItem('menu', JSON.stringify(this.menu));
          }
        })
        .catch(err => { });
    },

    methods: {
      // 退出系统
      quit () {
        this.post(this.api.logout, {
          loginId: this.userId,
          token: this.token
        }).then(res => {
          if (res.data.code === '200') {
            this.$router.push({ name: 'login' });
            localStorage.clear(); // 清除localStorage存储数据
            sessionStorage.clear();
            this.$Message.success('退出成功!');
          }
        });
      },
      // 保存连接的激活地址
      saveNavState (activePath) {
        window.sessionStorage.setItem('activePath', activePath);
      },
    },
  };
</script>

<style lang="scss" >
  .el-menu {
    border-right: none;
    .el-submenu {
      border-bottom: 1px solid rgb(41, 44, 54);
      .el-menu-item,
      .el-submenu__title {
        height: 50px;
        line-height: 50px;
      }
    }
  }

  .layout {
    // border: 1px solid #d7dde4;
    background: #f5f7f9;
    position: relative;
    // border-radius: 4px;
    overflow: hidden;
  }

  .layout-logo {
    width: 100px;
    height: 30px;
    background: #5b6270;
    border-radius: 3px;
    float: left;
    position: relative;
    top: 15px;
    left: 20px;
  }

  .layout-nav {
    width: 420px;
    margin: 0 auto;
    margin-right: 20px;
  }

  .layout-footer-center {
    text-align: center;
  }
  .el-menu-item.is-active {
    color: #33c99e !important;
    font-weight: bold;
  }
</style>
